import storage from '@/utils/helpers/storage'
import utils from '@/utils/lodash'
// 统一管理localStorage,只是监听不是拦截操作

const storePlugin = () => store => {
	store.subscribe((mutation, state) => {
		// 监听mutation,数据形式为{mutation名称：state对应名}会被本地存储
		const storageObj = {
			['SET_TOKEN']: 'token',
			['SET_USERINFO']:'userInfo'
		}
		let {
			type,
			payload
		} = mutation
		let finalType
		if (type.indexOf('/') >= 0) {
			finalType = type.split('/')[1]
		} else {
			finalType = type
		}
		// 将匹配到的值存储到storage中
		if (Object.keys(storageObj).includes(finalType)) {
			// console.log(finalType)
			storage.set(storageObj[finalType], payload)
			if (utils.isEmpty(payload)) {
				storage.remove(storageObj[finalType])
			}
		}
	})
}
export default storePlugin
